#pragma once
#include <thread>
#include <memory>
#include <boost/asio.hpp>
#include <functional>

class UDPServer {
public:
	typedef std::function<void(const std::string&)> EHFun;
	typedef std::function<void(const std::string&)> DHFun;

	UDPServer();
	~UDPServer();

	void start(const std::string& ip, const unsigned short port);
	void send(const std::string& data);
	void receiveHandle(const boost::system::error_code& error, std::size_t bytes_transferred);
	void bindErrorHandle(EHFun fun);
	void bindDataHandle(DHFun fun);

	EHFun m_eHFunc;
	DHFun m_dHFunc;
private:
	std::unique_ptr<std::thread, void(*)(std::thread*)> m_thread;
	boost::asio::io_service m_io;
	boost::asio::ip::udp::socket m_sock;
	boost::asio::deadline_timer m_dt;
	char m_buf[1024];
	std::string m_ip;
	unsigned short m_port;
	boost::asio::ip::udp::endpoint m_sender;
};

